export const baseUrl = 'https://api.sdast.org/user/v1/'
function getHeaders () {
    let token = getToken()
    let header = {
        "token":token,// 'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json;charset=UTF-8'
    }
	return header
}
function getHeadersf () {
    let token = getToken()
    let header = {
        "token":token,// 'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }
	return header
}
function fujiangetHeadersf () {
    let token = getToken()
    let header = {
        "token":token,// 'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'multipart/form-data;'
    }
	return header
}

export default{
    get(url,param){
        return new Promise((resolve,reject) =>{
            uni.request({
                method:'GET',
                url: baseUrl+url,
                data: param,
                header:getHeaders()
            }).then(res=>{
				console.log(res[1].data);
                switch (res[1].data.code) {
                  case 200:
                    resolve(res)
                    break
                  case 400:
                    resolve(res)
                    break
                  case 401:
                    resolve(res)
                    break
                  default:
                    reject(res)
                }    
            }).catch(
                (response) => {
                    reject(response)
                }
            )
        })
    },
	getf(url,param){
	    return new Promise((resolve,reject) =>{
	        uni.request({
	            method:'GET',
	            url: baseUrl+url,
	            data: param,
	            header:getHeadersf()
	        }).then(res=>{
	            switch (res[1].statusCode) {
	              case 200:
	                resolve(res[1])
	                break
	              case 400:
	                resolve(res[1])
	                break
	              case 401:
	                  resolve(res[1])
	                  break                        
	              default:
	                reject(res)
	            }            
	        }).catch(
	            (response) => {
	                reject(response)
	            }
	        )
	    })        
	},	
    post(url,param){
        return new Promise((resolve,reject) =>{
            uni.request({
                method:'POST',
                url: baseUrl+url,
                data: param,
                header:getHeaders()
            }).then(res=>{
                switch (res[1].statusCode) {
                  case 200:
                    resolve(res[1])
                    break
                  case 400:
                    resolve(res[1])
                    break
                  case 401:
                      resolve(res[1])
                      break                        
                  default:
                    reject(res)
                }            
            }).catch(
                (response) => {
                    reject(response)
                }
            )
        })        
    },
	postf(url,param){
	    return new Promise((resolve,reject) =>{
	        uni.request({
	            method:'POST',
	            url: baseUrl+url,
	            data: param,
	            header:getHeadersf ()
	        }).then(res=>{
	            switch (res[1].statusCode) {
	              case 200:
	                resolve(res[1])
	                break
	              case 400:
	                resolve(res[1])
	                break
	              case 401:
	                  resolve(res[1])
	                  break                        
	              default:
	                reject(res)
	            }            
	        }).catch(
	            (response) => {
	                reject(response)
	            }
	        )
	    })        
	},
	put(url,param){
	    return new Promise((resolve,reject) =>{
	        uni.request({
	            method:'PUT',
	            url: baseUrl+url,
	            data: param,
	            header:getHeadersf()
	        }).then(res=>{
	            switch (res[1].statusCode) {
	              case 200:
	                resolve(res[1])
	                break
	              case 400:
	                resolve(res[1])
	                break
	              case 401:
	                  resolve(res[1])
	                  break                        
	              default:
	                reject(res)
	            }            
	        }).catch(
	            (response) => {
	                reject(response)
	            }
	        )
	    })        
	},
	imgpost(url,param){
	    return new Promise((resolve,reject) =>{
	        uni.request({
	            method:'POST',
	            url: baseUrl+url,
	            data: param,
	            header:fujiangetHeadersf()
	        }).then(res=>{
	            switch (res[1].statusCode) {
	              case 200:
	                resolve(res[1])
	                break
	              case 400:
	                resolve(res[1])
	                break
	              case 401:
	                  resolve(res[1])
	                  break                        
	              default:
	                reject(res)
	            }            
	        }).catch(
	            (response) => {
	                reject(response)
	            }
	        )
	    })        
	},
}
export function getToken () {
    try {
        const userInfo = uni.getStorageSync('userInfo');
        if (userInfo) {
            return userInfo.token;
        }else{
            return ''
        }
    } catch (e) {
        console.log(e)
    }
}